package com.ruoyi.member.service.impl;


import java.util.List;

import com.ruoyi.common.constant.DicConstants;
import com.ruoyi.common.core.domain.entity.Member;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.member.mapper.MemberMapper;
import com.ruoyi.member.service.IMemberService;

/**
 * 客户信息Service业务层处理
 *
 * @author jiuyou
 * @date 2021-05-20
 */
@Service
public class MemberServiceImpl implements IMemberService
{
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ISysConfigService sysConfigService;
    /**
     * 查询客户信息
     *
     * @param id 客户信息ID
     * @return 客户信息
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }
    /**
     * 查询客户信息
     *
     * @param openId 客户信息openId
     * @return 客户信息
     */
    @Override
    public Member selectMemberByOpenId(String openId)
    {
        return memberMapper.selectMemberByOpenId(openId);
    }

    /**
     * 查询客户信息列表
     *
     * @param member 客户信息
     * @return 客户信息
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        member.setState(DicConstants.SYS_Y.getCode());
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增客户信息
     *
     * @param member 客户信息
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        member.setCreateTime(DateUtils.getNowDate());
        return memberMapper.insertMember(member);
    }

    /**
     * 修改客户信息
     *
     * @param member 客户信息
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        member.setUpdateTime(DateUtils.getNowDate());
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除客户信息
     *
     * @param ids 需要删除的客户信息ID
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除客户信息信息
     *
     * @param id 客户信息ID
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }

    @Override
    public String converWechatOpenId(String code) {
        String appId = sysConfigService.selectConfigByKey("app_id");
        String appSecret = sysConfigService.selectConfigByKey("app_secret");
        String url = "https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + appId + "&" +
                "secret=" + appSecret + "&" +
                "js_code=" + code + "&" +
                "grant_type=authorization_code";
        String content = HttpUtils.doGet(url, "UTF-8", 100, 1000);
       // System.out.println("C端用户"+content);
        return content;
    }

    @Override
    public Member selectMemberByOpenIdOrPhone(String openid, String purePhoneNumber) {
        return memberMapper.selectMemberByOpenIdOrPhone (openid,purePhoneNumber);
    }

    @Override
    public void loginOut(Long id) {
        memberMapper.loginOut(id);
    }

    @Override
    public Member selectMemberByPhone(String phone) {
        return memberMapper.selectMemberByPhone(phone);
    }

    @Override
    public List<Member> selectMemberByIds(List<Long> collect) {
        return memberMapper.selectMemberByIds(collect);
    }
}
